package com.lagou.edu.ctrl;

import com.lagou.api.CodeServcie;
import com.lagou.edu.dao.AuthTokenDao;
import com.lagou.edu.dao.UserDao;
import com.lagou.edu.feign.CodeServiceFeignClient;
import com.lagou.edu.pojo.AuthToken;
import com.lagou.edu.pojo.User;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/api/user")
public class UserCtrl {

  /*@Autowired
  CodeServiceFeignClient codeServiceFeignClient;*/

  @Reference
  private CodeServcie codeService;

  @Autowired
  AuthTokenDao authTokenDao;
  @Autowired
  UserDao userDao;


  @PostMapping("/register/{email}/{password}/{code}")
  public Boolean reg(@PathVariable String email,
    @PathVariable String password,@PathVariable String code,
    HttpServletResponse response) {

    if (StringUtils.isEmpty(email)
      || StringUtils.isEmpty(password)
      || StringUtils.isEmpty(code)
    ) {
      return false;
    }
    //validate code
    boolean rst = codeService.validate(email, code);

    //reg user
    if (rst) {
      if (!isRegistered(email)) {
        userDao.save(new User().setEmail(email).setPassword(password));

        genToken(email, response);
      }
    }
    return rst;
  }


  @PostMapping("/login/{email}/{password}")
  public Boolean login(@PathVariable String email, @PathVariable String password,
    HttpServletResponse response) {

    if (StringUtils.isEmpty(email)) {
      return false;
    }
    if (StringUtils.isEmpty(password)) {
      return false;
    }

    if (check(email,password)) {
      genToken(email, response);
      return true;
    }
    return false;
  }

  private void genToken(@PathVariable String email,
    HttpServletResponse response) {
    String token = UUID.randomUUID().toString();
    //gen token
    authTokenDao.save(new AuthToken().setEmail(email).setToken(token));
    //set cookies
    Cookie cookie =new Cookie("token",token);
    cookie.setPath("/");
    cookie.setMaxAge(7*24*60*60);
    response.addCookie(cookie);
  }


  private boolean check(String email, String password) {
    return userDao.exists(Example.of(new User().setEmail(email).setPassword(password)));
  }


  @GetMapping("/isRegistered/{email}")
  public boolean isRegistered(@PathVariable String email) {

    if (StringUtils.isEmpty(email)) {
      return false;
    }
    //check exist

    return userDao.exists(Example.of(new User().setEmail(email)));
  }



  @GetMapping("/info/{token}")
  public Boolean  list(@PathVariable String  token){

    return authTokenDao.exists(Example.of(new AuthToken().setToken(token)));
  }

}
